import { isRef } from 'vue'
import { useResizeObserver } from '@vueuse/core'

let elHeight = 0

export const useUpdateScrollBar = (elRef, scrollbarRef) => {
  if (!isRef(elRef) || !isRef(scrollbarRef)) {
    throw new Error('elRef or scrollbarRef must be ref')
  }
  return useResizeObserver(elRef, (entries) => {
    const { height } = entries[0].contentRect
    if (elHeight !== height) {
      elHeight = height
      scrollbarRef.value?.update()
    }
  })
}
